//
//  EPWebV2ViewController.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/12/28.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "EPWebV2ViewController.h"

@interface EPWebV2ViewController () <UIWebViewDelegate>

/// 模型视图
@property (nonatomic, readonly, strong) EPWebV2ViewModel* viewModel;

//IBOutlet
@property (weak, nonatomic) IBOutlet UIWebView* webView;

@end

@implementation EPWebV2ViewController
@dynamic viewModel;

- (instancetype)initWithViewModel:(SUViewModel2 *)viewModel
{
    self = [super initWithViewModel:viewModel];
    if (self)
    {
        [self initData];
        [self initLoadForRAC];
        [self initUI];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initData
{
    
}

- (void)initLoadForRAC
{
    @weakify(self)
    [[self rac_signalForSelector:@selector(viewDidLoad)] subscribeNext:^(id x) {
        @strongify(self)
        
    }];
}

- (void)initUI
{
    
}

- (void)bindViewModel
{
    [super bindViewModel];
    
    @weakify(self)
    
    RACSignal* willAppearSignal = [[self rac_signalForSelector:@selector(viewWillAppear:)] replay];
    RACSignal* didAppearSignal = [[self rac_signalForSelector:@selector(viewDidAppear:)] replay];
    RACSignal* viewDidLayoutSVSignal = [[self rac_signalForSelector:@selector(viewDidLayoutSubviews)] replay];
    
    [willAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
    }];
    
    [didAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
        // ScrollView 内容 未能在此时才约束
        
        //Request
        
        //保存操作
    }];
    
    [[self rac_signalForSelector:@selector(viewWillDisappear:)] subscribeNext:^(id x) {
        @strongify(self)
        
    }];
    
    [[[viewDidLayoutSVSignal take:1] deliverOnMainThread] subscribeNext:^(id x) {
        @strongify(self)
        
        //重置 更新 frame
        [self _setupWebView];
    }];
}

#pragma mark - 更新初始化 操作
- (void)_setupWebView
{
    if ([self.viewModel.webUrl isNotBlank])
    {
        self.title = self.viewModel.webName;
        
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.viewModel.webUrl]]];
    }
    else if ([self.viewModel.fileUrl isNotBlank])
    {
        self.title = self.viewModel.fileName;
        
        NSData* data = [NSData dataWithContentsOfFile:self.viewModel.fileUrl];
        
        if ([self.viewModel.fileType isEqualToString:@"docx"])
        {
            [self.webView loadData:data MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:@"UTF-8" baseURL:nil];
        }
        else
        {
            [self.webView loadData:data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:nil];
        }
    }
    [self.webView setScalesPageToFit:YES];
}

#pragma mark - Delegate 一些无法用RAC实现
// UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}

#pragma mark-代理方法
// 设立实现代理，注意要返回UIModalPresentationNone
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    return UIModalPresentationNone;
}

#pragma mark - 功能

#pragma mark - 跳转

@end
